<?php
	if (isset($_GET['cacheDel'])){
		$smarty->clear_compiled_tpl();
		$smarty->clear_all_cache();
	}
	
	if (isset($setupdata['smarty_user_plugins_dir'])){
		$p_dir_tmp = explode("/", $setupdata['smarty_user_plugins_dir']);
		unset($p_dir_tmp[count($p_dir_tmp)-1]);
		$p_dir = implode("/", $p_dir_tmp);
		if (file_exists($p_dir."/translate.php")) include($p_dir."/translate.php");
		unset($p_dir_tmp); unset($p_dir);
	}
	
	if (isset($_GET['corexajax'])){
		$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
		$smarty->register_object("ext2", $smarty_ext2);
		$smarty->assign("_http_path", $setupdata['base_href']);
		$smarty->register_block('dynamic', 'smarty_block_dynamic', false);
		$xajax->processRequests();	
		$mysqlclass->close_db();
		die();
	}
	
	if (file_exists($setupdata['core_dir']."/is_update.php")) include($setupdata['core_dir']."/is_update.php");
	
	$_SESSION['result'] = 0;
	
	$treeclass->optimize_tabes();
	
	if (isset($setupdata['vars']['translate']) && (strlen($setupdata['vars']['translate'])>0)){
		$setupdata['vars']['options']['translate'] = 1;
	}
	
	if (isset($setupdata['google_sitemap']) && (strlen($setupdata['google_sitemap'])>0)){
		$setupdata['vars']['options']['sitemap']['google'] = 1;
	}

/*
	if ((method_exists($treeclass, 'check_product_key')) && ($treeclass->check_product_key() != "OK")){
		$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
		$smarty->display('admin/cpr.tpl');
		$mysqlclass->close_db();
		die();
	}
*/
/*
	$handle = @fopen("http://www.technostudija.lt/.core/legal.php?site=".$setupdata['base_href'], "r");
	if ($handle){
		$content = "";
		while(!feof($handle)) $content .= fread($handle, 4096);
		if ($content != "OK!") {
			echo $content;
			die();
		}
		fclose($handle);
	} else {
		//echo "Please contact to info@technostudija.lt";
		//die();
	}
*/	

	if (isset($setupdata['strip']['admin'])){
		if ($setupdata['strip']['admin'] == 'hard') $smarty->register_prefilter("strip"); 	
		elseif ($setupdata['strip']['admin'] == 'lite') $smarty->load_filter('pre','trimwhitespace');
	}
	
	if (isset($setupdata['modules']['plugins']['path'])){
		$setupdata['vars']['plugins']['path'] = $setupdata['site_dir'].$setupdata['modules']['plugins']['path'];
	}
	
	if (isset($setupdata['smarty']['output']['admin']) && is_array($setupdata['smarty']['output']['admin'])){
		foreach ($setupdata['smarty']['output']['admin'] as $filter) $smarty->load_filter('output', $filter);
	}

	$smarty->caching = false;
	
	if (!(function_exists('login_ex'))){
		if (!isset($_COOKIE['interface_lang'])) {
			setcookie("interface_lang", "lt", time()+9999999);
			$_COOKIE['interface_lang'] = "lt";
		}

		$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
		$smarty->display('admin/login.tpl');
		$mysqlclass->close_db();
		die();
	}
	
	if (isset($setupdata['modules']['watermark']['is'])){
		$setupdata['vars']['watermark_path'] = $setupdata['binary_path']."/watermark";
	}
	
	if (!isset($_GET['act'])) $act=NULL; else $act=$_GET['act'];
	if ($act==NULL) if (!isset($_POST['act'])) $act=NULL; else $act=$_POST['act'];
	if (!isset($_GET['mod'])) $mod=NULL; else $mod=$_GET['mod'];
	if ($mod==NULL) if (!isset($_POST['mod'])) $mod=NULL; else $mod=$_POST['mod'];
	if (!isset($_GET['id']) || (isset($_GET['id']) && ($_GET['id']==''))) $id=0; else $id=(int) $_GET['id'];
	if ($id==NULL) if (!isset($_POST['id']) || (isset($_POST['id']) && ($_POST['id']==''))) $id=0; else $id=(int) $_POST['id'];	
	if (!isset($_GET['parent'])) $parent=NULL; else $parent=$_GET['parent'];
	if ($parent==NULL) if (!isset($_POST['parent'])) $parent=NULL; else $parent=$_POST['parent'];
 	if (!isset($_GET['name'])) $name=NULL; else $name=$_GET['name'];
	if ($name==NULL) if (!isset($_POST['name'])) $name=NULL; else $name=$_POST['name'];
 	if (!isset($_GET['type'])) $type=NULL; else $type=$_GET['type'];
	if ($type==NULL) if (!isset($_POST['type'])) $type=NULL; else $type=$_POST['type'];
 	if (!isset($_GET['hide'])) $hide=NULL; else $hide=$_GET['hide'];
	if ($hide==NULL) if (!isset($_POST['hide'])) $hide=NULL; else $hide=$_POST['hide'];
	if (!isset($_GET['depth'])) $depth=NULL; else $depth=$_GET['depth'];
	if ($depth==NULL) if (!isset($_POST['depth'])) $depth=NULL; else $depth=$_POST['depth'];
	if (!isset($_GET['pos'])) $pos=NULL; else $pos=$_GET['pos'];
	if ($pos==NULL) if (!isset($_POST['pos'])) $pos=NULL; else $pos=$_POST['pos'];
	if (!isset($_GET['group2'])) $group2=NULL; else $group2=$_GET['group2'];
	if ($group2==NULL) if (!isset($_POST['group2'])) $group2=NULL; else $group2=$_POST['group2'];
	if (!isset($_GET['group1'])) $group1=NULL; else $group1=$_GET['group1'];
	if ($group1==NULL) if (!isset($_POST['group1'])) $group1=NULL; else $group1=$_POST['group1'];
	if (!isset($_GET['dlink'])) $dlink=NULL; else $dlink=$_GET['dlink'];
	if ($dlink==NULL) if (!isset($_POST['dlink'])) $dlink=NULL; else $dlink=$_POST['dlink'];
	if (!isset($_GET['show_menu'])) $show_menu=NULL; else $show_menu=$_GET['show_menu'];
	if ($show_menu==NULL) if (!isset($_POST['show_menu'])) $show_menu=NULL; else $show_menu=$_POST['show_menu'];
	if (!isset($_GET['id_type'])) $id_type=NULL; else $id_type=$_GET['id_type'];
	if ($id_type==NULL) if (!isset($_POST['id_type'])) $id_type=NULL; else $id_type=$_POST['id_type'];

	if (!isset($_GET['login'])) $login=NULL; else $login=$_GET['login'];
	if ($login==NULL) if (!isset($_POST['login'])) $login=NULL; else $login=$_POST['login'];
	if (!isset($_GET['fname'])) $fname=NULL; else $fname=$_GET['fname'];
	if ($fname==NULL) if (!isset($_POST['fname'])) $fname=NULL; else $fname=$_POST['fname'];
	if (!isset($_GET['lname'])) $lname=NULL; else $lname=$_GET['lname'];
	if ($lname==NULL) if (!isset($_POST['lname'])) $lname=NULL; else $lname=$_POST['lname'];
	if (!isset($_GET['pass1'])) $pass1=NULL; else $pass1=$_GET['pass1'];
	if ($pass1==NULL) if (!isset($_POST['pass1'])) $pass1=NULL; else $pass1=$_POST['pass1'];
	if (!isset($_GET['pass2'])) $pass2=NULL; else $pass2=$_GET['pass2'];
	if ($pass2==NULL) if (!isset($_POST['pass2'])) $pass2=NULL; else $pass2=$_POST['pass2'];
	if (!isset($_GET['email'])) $email=NULL; else $email=$_GET['email'];
	if ($email==NULL) if (!isset($_POST['email'])) $email=NULL; else $email=$_POST['email'];
	if (!isset($_GET['prior'])) $prior=NULL; else $prior=$_GET['prior'];
	if ($prior==NULL) if (!isset($_POST['prior'])) $prior=NULL; else $prior=$_POST['prior'];
	if (!isset($_GET['login_id'])) $login_id=NULL; else $login_id=$_GET['login_id'];
	if ($login_id==NULL) if (!isset($_POST['login_id'])) $login_id=NULL; else $login_id=$_POST['login_id'];
	if (!isset($_GET['ad'])) $ad=NULL; else $ad=$_GET['ad'];
	if ($ad==NULL) if (!isset($_POST['ad'])) $ad=NULL; else $ad=$_POST['ad'];
	if (!isset($_GET['permission'])) $permission=NULL; else $permission=$_GET['permission'];
	if ($permission==NULL) if (!isset($_POST['permission'])) $permission=NULL; else $permission=$_POST['permission'];

	if (!isset($_GET['image_id'])) $image_id=NULL; else $image_id=$_GET['image_id'];
	if ($image_id==NULL) if (!isset($_POST['image_id'])) $image_id=NULL; else $image_id=$_POST['image_id'];
	if (!isset($_GET['field_name'])) $field_name=NULL; else $field_name=$_GET['field_name'];
	if ($field_name==NULL) if (!isset($_POST['field_name'])) $field_name=NULL; else $field_name=$_POST['field_name'];
	
	if (!isset($_GET['new_lang'])) $new_lang=NULL; else $new_lang=$_GET['new_lang'];
	if ($new_lang==NULL) if (!isset($_POST['new_lang'])) $new_lang=NULL; else $new_lang=$_POST['new_lang'];

	if (!isset($_GET['ad'])) $ad=0; else $ad=$_GET['ad'];
	if ($ad==0) if (!isset($_POST['ad'])) $ad=0; else $ad=$_POST['ad'];

	if (isset($_GET['body'])) $_SESSION['body'] = $_GET['body'];
	if (!isset($_POST['body']) and isset($_POST['body'])) $_SESSION['body'] = $_POST['body'];
	if (isset($_POST['form_filter'])){
		if ($_POST['form_filter']['action'] == "reset") unset($_SESSION['form_filter']);
		else $_SESSION['form_filter'] = $_POST['form_filter'];
	}

	include($setupdata['core_dir']."/includes/setlang.php");

	$setupdata['help_tips_status'] = $treeclass->get_help_tips_status($lang);

	if (!isset($_GET['module'])) $module=NULL; else $module=$_GET['module']; 
	if ($module==NULL) if (!isset($_POST['module'])) $module=NULL; else $module=$_POST['module']; 
	
	if (($module != NULL) && (isset($setupdata['modules'][$module]['is'])) && (isset($setupdata['modules']['plugins']['path']) && file_exists($plugin_dir."/modules/".$module."/".$module."_admin.php"))){
		include $plugin_dir."/modules/".$module."/".$module."_admin.php";
		$module = true;
	} elseif (($module != NULL) && (isset($setupdata['modules'][$module]['is'])) && (file_exists($setupdata['core_dir']."/modules/".$module."/".$module."_admin.php"))) {
		include $setupdata['core_dir']."/modules/".$module."/".$module."_admin.php";
		$module = true;
	} else {
		if ($mod=="admins"){
			$us = $usersclass->GetUserInfo($_SESSION['login_user'], "admins");
			if ($act == 'add') {
				if ($us['mod'][1] == 1) {
						if ((strlen($login) > 1) and ($pass1 == $pass2) and (strlen($pass1) > 1)) {
							if ($pass1 == $pass2) {
								$e = $usersclass->AddUserInfo($login, $pass1, "admins", $permission);
								if ($e['error'] != "") $err->AddError($e);
								else {
									$dat=$treeclass->gettreetypes('admins01');
									foreach ($dat as $value){
										$n = $value['name'];
										$t = $value['type_of_value'];
										if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
										$v = $treeclass->post_text_parsing($v);
										if ((isset($_GET[$n])) or (isset($_POST[$n]))) {
											$usersclass->EditClientInfo($e['parent'], $n, $v, $t, $dat);
										}
									}
								}
							}
							else $err->AddError(3);
						} else $err->AddError(2);
				} else $err->AddError(4);
			} elseif ($act == 'mod') {
				if ($us['mod'][0] == 1) {
					if (($pass1 != $pass2) and ((strlen($pass1) > 0) or (strlen($pass2) > 0))) $err->AddError(3);
					else {
						if ($pass1 == $pass2) $pass = $pass1;
						if (ereg("[^a-zA-Z0-9_-]", $pass)) $err->AddError(2);
						if (ereg("[^a-zA-Z0-9_-]", $login)) $err->AddError(2);
						if (!ereg("[^a-zA-Z0-9_-]", $pass) && !ereg("[^a-zA-Z0-9_-]",$login)){
							if (strlen($login) > 1) $usersclass->ModUserInfo($login_id, $login, $pass, $permission);
							else $err->AddError(2);
							$dat=$treeclass->gettreetypes('admins01');
							foreach ($dat as $value){
								$n = $value['name'];
								$t = $value['type_of_value'];
								if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
								$v = $treeclass->post_text_parsing($v);
								if ((isset($_GET[$n])) or (isset($_POST[$n]))) {
									$usersclass->EditClientInfo($login_id, $n, $v, 'admins01', $dat);
								}
							}
						}
					}
				} else $err->AddError(4);
			} elseif ($act == 'del') {
				if ($us['mod'][1] == 1) {
					if ($ad > 0) $usersclass->DelClientInfo($ad);
					else $err->AddError(2);
				} else $err->AddError(4);
			} elseif ($act == 'save_page_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($new_lang) and (strlen($new_lang) > 0)){
						if (isset($_POST['perm_array'])) $treeclass->set_page_permision($login_id, $_POST['perm_array'], $new_lang);
						else $treeclass->set_page_permision($login_id, NULL, $new_lang);
					} else {
						if (isset($_POST['perm_array'])) $treeclass->set_page_permision($login_id, $_POST['perm_array'], $lang);
						else $treeclass->set_page_permision($login_id, NULL, $lang);
					}
				} else $err->AddError(4);
			} elseif ($act == 'save_site_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($_POST['perm_array'])) $treeclass->set_site_permision($login_id, $_POST['perm_array']);
					else $treeclass->set_site_permision($login_id, NULL);
				} else $err->AddError(4);
			} elseif ($act == 'save_shop_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($_POST['perm_array'])) $shopclass->set_user_permision($login_id, $_POST['perm_array']);
					else $shopclass->set_user_permision($login_id, NULL);
				} else $err->AddError(4);
			} elseif ($act == 'save_realstate_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($_POST['perm_array'])) $realstateclass->set_user_permision($login_id, $_POST['perm_array']);
					else $realstateclass->set_user_permision($login_id, NULL);
				} else $err->AddError(4);
			} elseif ($act == 'save_docum_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($_POST['perm_array'])) $documclass->set_user_permision($login_id, $_POST['perm_array']);
					else $documclass->set_user_permision($login_id, NULL);
				} else $err->AddError(4);
			} elseif ($act == 'save_contacts_permis') {
				if ($us['mod'][1] == 1) {
					if (isset($_POST['perm_array'])) $contactsclass->set_user_permision($login_id, $_POST['perm_array']);
					else $contactsclass->set_user_permision($login_id, NULL);
				} else $err->AddError(4);
			}
		} elseif ($mod=="forms"){
			if ($act == 'change_status') {
				$treeclass->change_form_status($parent, $depth, $_SESSION['login_user']);
			}
		} elseif ($mod=="savetranslate"){
			if ((isset($_POST['data']) && (count($_POST['data']) > 0))){
				$smarty_ext->save_print2_translate($_POST['data'], $_SESSION['lang']);
			}
		} elseif ($mod=="lang_on"){
			$lang_on = "";
			if (isset($_GET['lang_on'])) $lang_on=$_GET['lang_on'];
			if ($lang_on=="") if (isset($_POST['lang_on'])) $lang_on=$_POST['lang_on'];
			if (($lang_on != "") && ($lang_on != "on")) $lang_on = "off";
			$treeclass->save_lang_status($lang, $lang_on);
			unset($lang_on);
		} elseif ($mod=="comments"){
			if ($act == 'mod_comment') {
				if (isset($_POST['comment_nr'])){
					$comm_mas = array();
					$comm_type_array = $treeclass->get_comments_fields($parent);
					foreach($_POST as $key=>$el){
						foreach($comm_type_array as $k=>$field){
							if ($field['name'] == $key) {
								if ($field['type_of_value'] == "date"){
									$tmp = explode(".",  $el);
									$el = mktime($tmp[3], $tmp[4], $tmp[5], $tmp[1], $tmp[2], $tmp[0]);
									if ($el == -1) $el = 0;
									$comm_mas[$key]['name'] = $comm_type_array[$k]['name'];
									$comm_mas[$key]['type'] = $comm_type_array[$k]['type_of_value'];
									$comm_mas[$key]['value'] = $el;
								} else {
									$comm_mas[$key]['name'] = $comm_type_array[$k]['name'];
									$comm_mas[$key]['type'] = $comm_type_array[$k]['type_of_value'];
									$comm_mas[$key]['value'] = $el;
								}
							}
						}
					}
					if (count($comm_mas) > 0) $treeclass->mod_coments_to_db($_POST['comment_nr'], $parent, $comm_mas);
					unset($comm_mas);
				}
			} elseif ($act == 'add_comment') {
				foreach($_POST as $key=>$el){
					if ((strpos($key, "comments_data_") !== false) and (strpos($key, "comments_data_") == 0)) $comments_data[$key] = $el;
				}
				if (count($comments_data) > 0) $treeclass->save_coments_to_db($parent, $comments_data);
			} elseif ($act == 'del_comment_mas') {
				$del_mas = array();
				if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
				elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
				if (count($del_mas) > 0){
					$treeclass->del_comment_from_db($del_mas);
				}
			} elseif ($act == 'set_cenzure') {
				if (isset($_GET["nr"])) $nr=$_GET["nr"]; else $nr = NULL;
				if (isset($_GET["action"])) $action=$_GET["action"]; else $action = NULL;
				if (($nr != NULL) && ($action != NULL)){
					$treeclass->set_comment_cenzure($id, $nr, $action);
				}
			}
		} elseif ($mod=="catalog"){
			if (($setupdata['vars']['keywords'] == 1) && ($act == 'metatags')){
				$treeclass->save_keywords();
			} elseif ($act == 'add_block') {
				$p = $treeclass->set_block_parent($parent, $name, $id, $lang, $dlink);
			} elseif ($act == 'block_copy') {
				$copy_mas = array();
				if (isset($_GET["ids"])) $copy_mas=$_GET["ids"];
				elseif (isset($_POST["ids"])) $copy_mas=$_POST["ids"];
				if (is_array($copy_mas) && (count($copy_mas) > 0)){
					foreach ($copy_mas as $v) {
						$treeclass->tree_copy($v, 0, $_SESSION['login_user'], $parent);
					}
				}
			} elseif (($act == 'down') || ($act == 'up')) {
				$p = $treeclass->treepos($act, $id);
			} elseif ($act == 'add') {
				if ((strlen($name) > 0) and (isset($type) and (strlen($type) > 0))){
					if (strlen($group2) > 0) $gr = $group2;
					else $gr = $group1;
					$gr=str_replace (" ","_",$gr);
					if (($treeclass->get_id_by_dlink($dlink, $lang) == 0) || ($dlink=="")){
						$p = $treeclass->treenew($parent, $name, $type, $hide, $depth, $pos, $lang, $gr, $dlink, $_SESSION['login_user']);
						$treeclass->set_child_permision($parent, $p);
						if ((strpos($type, "block")===true) and (strpos($type, "block")==0)) $smarty->assign("goto", "");
						else $smarty->assign("goto", "id=".$p);
						$dat = $treeclass->gettreetypes($type);
						$dat = $treeclass->unique_multi_array($dat, 'name');
						if (count($dat) > 0){
							foreach ($dat as $value){
								$n = $value['name'];
								$p2 = $treeclass->save_item_data($type, $p, $n, '');
							}
						}
					} else $err->AddError(16);
				} else $err->AddError(5);
			} elseif ($act == 'del_undo') {		
				$p = $treeclass->treedel($id);
			} elseif ($act == 'del_block_mas'){
				$del_mas = array();
				if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
				elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
				if (count($del_mas) > 0){
					$treeclass->del_block($del_mas);
				}
			} elseif ($act == 'del_mas_undo') {
				$del_mas = array();
				if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
				elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
				if (count($del_mas) > 0){
					krsort($del_mas, SORT_NUMERIC);
					foreach($del_mas as $key=>$value) {
						$p = $treeclass->treedel($value);
					}
				}
			} elseif ($act == 'del_form_mas') {
				$del_mas = array();
				if (isset($_GET["ids"])) $del_mas=$_GET["ids"];
				elseif (isset($_POST["ids"])) $del_mas=$_POST["ids"];
				if (count($del_mas) > 0){
					$treeclass->del_form_to_db($del_mas);
				}
			} elseif ($act == 'undo') {
				//$p = $treeclass->tree_undo($id, $_SESSION['login_user']);
			} elseif ($act == 'del') {
				$p = $treeclass->treedel($id);
			} elseif ($act == 'array_save') {
				if (isset($_POST['item']) && is_array($_POST['item']) && count($_POST['item']) > 0){
					foreach ($_POST['item'] as $k=>$v){
						if (is_array($v) && count($v) > 0){
							foreach ($v as $k1=>$v1){
								$treeclass->mod_item_data($k, $k1, $v1);
							}
						}
					}
				}
			} elseif ($act == 'mod') {
				if (strlen($name) > 0){
					$get_dlink_id = $treeclass->get_id_by_dlink($dlink, $lang);
					if (($get_dlink_id == 0) || ($get_dlink_id == $id) || ($dlink == "")){
						if (strlen($group2) > 0) $gr = $group2;
						else $gr = $group1;
						$treeclass->treemod($id, $name, $hide, $gr, $dlink, $_SESSION['login_user']);
						$p = $id;
					}  else $err->AddError(16);
				} else $err->AddError(5);
			} elseif ($act == 'hid') {
				$treeclass->treehide($id, $hide);
			} elseif ($act == 'mod_name') {
				$name = trim($name);
				if (strlen($name) > 0) {
					$treeclass->treemodname($id, $name, $_SESSION['login_user']);
				}
				$p = $id;
			}
		} elseif ($mod=="image"){
			if ($act == 'del') {
				$imageclass->image_del_with_parent_data($parent, $field_name, $image_id);
			}
		} elseif ($mod=="file"){
			if ($act == 'del') {
				$fileclass->file_del_with_parent_data($parent, $field_name, $image_id);
			}
		} elseif ($mod=="flash"){
			if ($act == 'del') {
				$imageclass->flash_del_with_parent_data($parent, $field_name, $image_id);
			}
		} elseif ($mod=="sitemap"){
			if ($act == 'google') {
				if (isset($setupdata['google_sitemap']) && (strlen($setupdata['google_sitemap'])>0)){
					$res = $treeclass->create_google_site_map($setupdata['google_sitemap'], 1);
					$_SESSION['sitemap']['result'] = $res;
				}
			}
		} elseif ($mod=="product"){
			if ($act == 'mod') {
				$dat=$treeclass->gettreetypes($id_type);
				if (isset($_GET['parent'])) $p=$_GET['parent']; else {if (isset($_POST['parent'])) $p=$_POST['parent'];}
				if (intval($p) > 0) $treeclass->treemodupdate($p, $_SESSION['login_user']);
				foreach ($dat as $value){
					$n = $value['name'];
					$t = $value['type_of_value'];
					if ($t == 'float'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if ($v == "") $v = 0;
						else $v = (float) $v;
					} else if ($t == 'number'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if ($v == "") $v = 0;
						else $v = (int) $v;
					} else if ($t == 'date'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if ($v == "") {
							$v = "";
						} else {
							$tmp = explode(".",  $v);
							$v = mktime($tmp[3], $tmp[4], $tmp[5], $tmp[1], $tmp[2], $tmp[0]);
							if ($v == -1) {
								$v = "";
							}
						}
					} else if ($t == 'vote'){
						if (isset($_GET['form_id'])) $form_id=$_GET['form_id']; else {if (isset($_POST['form_id'])) $form_id=$_POST['form_id']; else $form_id = "";}
						if (($form_id != "") and ($form_id > 0)){
							$treeclass->del_form_elem_data($form_id, 'vote');
							foreach($_POST['form_array'] as $name=>$value) {
								$value = trim($value);
								if (($value != '') && ($value != '0')) $treeclass->mod_form_elem_data($id, $form_id, $name, $value, 'vote');
							}
						}
					} else if ($t == 'form'){
						if (isset($_GET['form_id'])) $form_id=$_GET['form_id']; else {if (isset($_POST['form_id'])) $form_id=$_POST['form_id']; else $form_id = "";}
						if (($form_id != "") and ($form_id > 0)){
							$treeclass->del_form_elem_data($form_id, 'form');
							foreach($_POST['form_array'] as $name=>$value) {
								if (($value != 'empty') && ($value != "")) {
									$treeclass->mod_form_elem_data($id, $form_id, $name, $value, 'form');
								}
							}
						}
					} else if ($t == 'form3'){
						if (isset($_GET['form_id'])) $form_id=$_GET['form_id']; else {if (isset($_POST['form_id'])) $form_id=$_POST['form_id']; else $form_id = "";}
						if (($form_id != "") and ($form_id > 0)){
							$treeclass->del_form_elem_data($form_id, 'form');
							foreach($_POST['form_array'] as $name=>$value) {
								if ($value != 'empty') $treeclass->mod_form_elem_data($id, $form_id, $name, $value, 'form');
							}
						}
					} else if ($t == 'client_groups'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if (is_array($v)) $v = join(";", $v);
					} else if ($t == 'file'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if (isset($_GET["x_".$n])) $v=$_GET["x_".$n]; else {if (isset($_POST["x_".$n])) $v=$_POST["x_".$n]; else $v = "";}
						if ($v == "0"){
							$file = $_FILES[$n];
							$file_name1 = $_FILES[$n]['name'];
							$v = $file['tmp_name'];
							if ($v != ""){
								$today = uniqid("");
								$size = $file['size'];
								if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
								else {
									$name = $parent."-".$today."-".$size."-".$file['name'];
									$file_to = $name;
									$file_name = $name;
									if (strlen($file_name1) <= 0) $file_name1=$file['name'];
									if (!(file_exists($setupdata['binary_path']."/".$parent))){
										mkdir($setupdata['binary_path']."/".$parent, 0777);
										if (file_exists($setupdata['binary_path']."/".$parent)){
											if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
												if (file_exists( $setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$v = $fileclass->file_add($file, $file_name1, $file_to, $parent, $_SESSION['login_user']);
												}
											}
										}
									} else {
										if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
											if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
												chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
												$v = $fileclass->file_add($file, $file_name1, $file_to, $parent, $_SESSION['login_user']);
											}
										}
									}
								}
							}
						}						
					} else if ($t == 'flash'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if (isset($_GET["x_".$n])) $v=$_GET["x_".$n]; else {if (isset($_POST["x_".$n])) $v=$_POST["x_".$n]; else $v = "";}
						if ($v == "0"){
							$file = $_FILES[$n];
							$file_name1 =$_FILES[$n]['name'];
							$v = $file['tmp_name'];
							if ($v != ""){
								if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
								else {
									$imagedata = getimagesize($v);
									$today = uniqid("");
									$name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".swf";
									$file_name = $name;
									$file_to = $name;
									if (strlen($file_name1) <= 0) $file_name1=$file['name'];
									if (!(file_exists($setupdata['binary_path']."/".$parent))){
										mkdir($setupdata['binary_path']."/".$parent, 0777);
										if (file_exists($setupdata['binary_path']."/".$parent)){
											if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
												if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
													chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
													$v = $imageclass->flash_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
												}
											}				
										}
									} else {
										if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
											if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
												chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
												$v = $imageclass->flash_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
											}
										}
									}
								}
							}
						}						
					} else if ($t == 'image'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if (isset($_GET["x_".$n])) $v=$_GET["x_".$n]; else {if (isset($_POST["x_".$n])) $v=$_POST["x_".$n]; else $v = "";}
						if ($v == "0"){
							$file = $_FILES[$n];
							$file_name1=$_FILES[$n]['name'];
							$v = $file['tmp_name'];
							if ($v != ""){
								if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
								else {
									$file_name = "";
									$today = uniqid("");
									$imagedata = getimagesize($v);
									if (!isset($imagedata['mime'])){
										if ($imagedata[2] == 1) $imagedata['mime'] = 'image/gif';
										elseif ($imagedata[2] == 2) $imagedata['mime'] = 'image/jpeg';
                    elseif ($imagedata[2] == 3) $imagedata['mime'] = 'image/png';
                    elseif ($imagedata[2] == 6) $imagedata['mime'] = 'image/bmp';
									}
									if ($imagedata === FALSE) $err->AddError(7);
									else {
										if ($imagedata['mime'] == 'image/jpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpg";
										elseif ($imagedata['mime'] == 'image/pjpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpeg";
										elseif ($imagedata['mime'] == 'image/gif') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".gif";
                    elseif ($imagedata['mime'] == 'image/png') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".png";
                    elseif ($imagedata['mime'] == 'image/bmp') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".bmp";
										else $err->AddError(7);
										if (strlen($file_name) > 3){
											$file_to = $file_name;
											if (strlen($file_name1) <= 0) $file_name1=$_FILES['img_file1']['name'];
											if (!(file_exists($setupdata['binary_path']."/".$parent))){
												mkdir($setupdata['binary_path']."/".$parent, 0777);
												if (file_exists($setupdata['binary_path']."/".$parent)){
													if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
														if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
															chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
															$v = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
														}
													}
												}
											} else {
												if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
													if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
														chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
														$v = $imageclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
													}
												}
											}
										}
									}
								}
							}
						}
					} else if ($t == 'imagew'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if ($v == "0"){
							$file = $_FILES[$n];
							$file_name1=$_FILES[$n]['name'];
							$v = $file['tmp_name'];
							if ($v != ""){
								if (isset($setupdata['file_size']) && ($file['size']/1024 > $setupdata['file_size'])) $err->AddError(28);
								else {
									$file_name = "";
									$today = uniqid("");
									$imagedata = getimagesize($v);
									if (!isset($imagedata['mime'])){
										if ($imagedata[2] == 1) $imagedata['mime'] = 'image/gif';
										elseif ($imagedata[2] == 2) $imagedata['mime'] = 'image/jpeg';
									}
									if ($imagedata === FALSE) $err->AddError(7);
									else {
										if ($imagedata['mime'] == 'image/jpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpg";
										elseif ($imagedata['mime'] == 'image/pjpeg') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".jpeg";
										elseif ($imagedata['mime'] == 'image/gif') $file_name = $parent."-".$today."-".$imagedata[0]."x".$imagedata[1].".gif";
										else $err->AddError(7);
										if (strlen($file_name) > 3){
											$file_to = $file_name;
											if (strlen($file_name1) <= 0) $file_name1=$_FILES['img_file1']['name'];
											if (!(file_exists($setupdata['binary_path']."/".$parent))){
												mkdir($setupdata['binary_path']."/".$parent, 0777);
												if (file_exists($setupdata['binary_path']."/".$parent)){
													if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
														if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
															chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
															$v = $watermarkclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
														}
													}
												}
											} else {
												if (copy($file['tmp_name'], $setupdata['binary_path']."/".$parent."/".$file_to)){
													if (file_exists($setupdata['binary_path']."/".$parent."/".$file_to)){
														chmod($setupdata['binary_path']."/".$parent."/".$file_to, 0666);
														$v = $watermarkclass->image_add($file, $file_name1, $file_to, $parent, $imagedata[0], $imagedata[1], $_SESSION['login_user']);
													}
												}
											}
										}
									}
								}
							}
						}
					} else if ($t == 'select_customer'){
						if (isset($_GET[$n."_text"])) $v=$_GET[$n."_text"]; else {if (isset($_POST[$n."_text"])) $v=$_POST[$n."_text"]; else $v = "";}
						if ($v == ""){
							if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						}
					} else if ($t == 'super_select'){
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						if (($v != "") && is_array($v)){
							$v = $treeclass->mod_super_select($p, $n, $v);
						} else $v = "";
					} else if (($t == 'docum') || ($t == 'smalldocum')){ 
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
						//$v = $treeclass->image_parsing_resize($v);
					} else if (($t == 'checkbox_elements_from')){
						if (isset($_POST[$n]) && is_array($_POST[$n]) && (count($_POST[$n]) > 0)) {
							$v="";
							foreach ($_POST[$n] as $key=>$value){
								$v .= $key.";";
							}
						}
						else $v = "";
					} else { 
						if (isset($_GET[$n])) $v=$_GET[$n]; else {if (isset($_POST[$n])) $v=$_POST[$n]; else $v = "";}
					}
					if ((isset($_GET[$n])) or (isset($_POST[$n]))) {
						if (($t != 'form') && ($t != 'form3')){
							$v = $treeclass->post_text_parsing($v);
							if ((($t == 'image') || ($t == 'file') || ($t == 'imagew') || ($t == 'flash')) && (!isset($_POST["x_".$n]))) {
								if (isset($file_name) and (strlen($file_name) > 3)) $treeclass->mod_item_data($p, $n, $v);
							} else {
								$treeclass->mod_item_data($p, $n, $v);
							}
						}
					} else {
						if ($t == 'chekbox') $treeclass->mod_item_data($p, $n, "");
					}
				}
			}
		}
		
		if ($show_menu=="galer"){
			$data['galer'] = $imageclass->images_sel($id);
		}
	}

	$_SESSION['target'] = $mod;
	$_SESSION['action'] = $act;

	$data['path'] = $treeclass->sqlpath($id);

	$data['new']['pos'] = 1;
	$data['tree']['types']=$treeclass->gettreetypes('tree');
	$data['tree']['childtypes']=$treeclass->getchildtypes($id, 'element');
	$data['tree']['lang']=$treeclass->gettreetypes('lang');
	$data['tree']['group']=$treeclass->gettreegroup();
	$data['tree']['parent']=$treeclass->gettreeparent($id);

	function get_tree_mas($new_lang, $setupdata, $lang){
		global $treeclass, $data;
		$new_lang2 = NULL;
		if ($new_lang != NULL) {
			foreach($data['tree']['lang'] as $value) {
				if (($new_lang2 == NULL) and (strtolower($value['name']) == strtolower($new_lang))) $new_lang2 = strtolower($new_lang);
			}
		}
		if ($setupdata['tree_cache'] == 0){
			if (isset($setupdata['local_vars']['last_db_update'])) $last_update_db_time = $setupdata['local_vars']['last_db_update'];
			else $last_update_db_time = $treeclass->get_last_update_db();
			if (isset($setupdata['local_vars']['tree_cache_time'])) $tree_cache_time = $setupdata['local_vars']['tree_cache_time'];
			else $tree_cache_time = $treeclass->get_tree_cache_time('admin');
			if (isset($setupdata['cache']['tree']['admin']['type']) && isset($setupdata['cache']['tree']['admin']['path'])){
				$cached_tree = $treeclass->get_cached_tree($setupdata['cache']['tree']['admin']['type'], $setupdata['cache']['tree']['admin']['path'], 'admin');
			} else $cached_tree = $treeclass->get_cached_tree('0', '0', 'admin');
			if (($last_update_db_time > $tree_cache_time) || (!is_array($cached_tree))){
				$lang_array1 = $treeclass->gettreetypes('lang');
				$tree_types = $treeclass->gettreetypes('tree');
				foreach ($lang_array1 as $value) {
					$treeclass->maketree(0, strtolower($value['name']), $tree_types);
					$tree_all[strtolower($value['name'])]=$treeclass->mas;
				}
				if (isset($setupdata['cache']['tree']['admin']['type']) && isset($setupdata['cache']['tree']['admin']['path'])){
					$treeclass->save_cached_tree($tree_all, $setupdata['cache']['tree']['admin']['type'], $setupdata['cache']['tree']['admin']['path'], 'admin');
				} else $treeclass->save_cached_tree($tree_all, '0', '0', 'admin');
			} else {
				$tree_all = $cached_tree;
			}
		} else {
			$lang_array1 = $treeclass->gettreetypes('lang');
			$tree_types = $treeclass->gettreetypes('tree');
				foreach ($lang_array1 as $value) {
					$treeclass->maketree(0, strtolower($value['name']), $tree_types);
					$tree_all[strtolower($value['name'])]=$treeclass->mas;
				}
		}
		
		if ($new_lang2 != NULL)	$new_lang3 = $new_lang2;
		else $new_lang3 = $lang;

		while (!isset($tree_all[$new_lang3])){
			$setupdata['local_vars']['last_db_update'] = $setupdata['local_vars']['tree_cache_time'] + 10000;
			$tree_all = get_tree_mas($new_lang3, $setupdata, $lang);
		}
		
		$tree = $tree_all[$new_lang3];

		return $tree;
	}

/*	
	$tree_types = $treeclass->gettreetypes('tree');
	$new_lang2 = NULL;
	if ($new_lang != NULL) {
		foreach($data['tree']['lang'] as $value) {
			if (($new_lang2 == NULL) and (strtolower($value['name']) == strtolower($new_lang))) $new_lang2 = strtolower($new_lang);
		}
	}
	if ($new_lang2 != NULL)	$treeclass->maketree(0, $new_lang2, $tree_types);
	else $treeclass->maketree(0, $lang, $tree_types);
	$tree=$treeclass->mas;
*/
	$tree = get_tree_mas($new_lang, $setupdata, $lang);
	
	if ($mod=="search_in_tree"){
		if (!isset($_GET['query'])) $query=NULL; else $query=$_GET['query'];
		if ($query==NULL) if (!isset($_POST['query'])) $query=NULL; else $query=$_POST['query'];
		if ($query != NULL){
			$data['search_in_tree']['query'] = $query;
			$data['search_in_tree']['result'] = $treeclass->search_in_tree($query, $tree);
		} else {
			$data['search_in_tree']['query'] = "";
			$data['search_in_tree']['result'] = array();
		}
	}


	if ($id > 0) {
		if (count($data['path']) > 0) $data['page']=$data['path'][count($data['path'])-1];
		else $data['page'] = 0;
		$data['field']=$treeclass->gettreetypes($data['page']['type']);
		if (count($tree) > 0){
		   	$data['new']['pos']=$treeclass->treeparent($id)+1;
			$data['new']['depth']=$data['page']['depth']+1;
		} else {
			$data['new']['pos'] = $treeclass->treeparent(0)+1;
			$data['new']['depth']=0;
		}
		$data['elements']=$treeclass->_get_page_elements_with_content($id);
	} else { 
	 	$data['page']['id'] = 0;
		$data['new']['pos'] = $treeclass->treeparent(0)+1;
		$data['new']['depth'] = 0;
	}

	$data['admins']['all'] = $usersclass->GetUserInfoAll("admins");
	$data['admins']['cur'] = $usersclass->GetUserInfo($_SESSION['login_user'], "admins");
	
	if ($ad >= 0) {
		$data['admins']['curent'] = $usersclass->GetUserInfo($ad, "admins");
	}

/*
	$undo = $treeclass->get_last_undo($lang);
	foreach ($undo as $value) {
		if (strpos($value['type'], "del_")==0) $data['tree']['undo']=$value['id'];		
	}
*/
	if ($smarty_ext->get_site_settings_id() == 0){
		$p = $treeclass->treenew(0, 'site_settings01', 'site_settings01', 1, 0, 0, $_SESSION['lang'], "", "", $_SESSION['login_user']);
		$dat = $treeclass->gettreetypes('site_settings01');
		$dat = $treeclass->unique_multi_array($dat, 'name');
		if (count($dat) > 0){
			foreach ($dat as $value){
				$n = $value['name'];
				$p2 = $treeclass->save_item_data('site_settings01', $p, $n, '');
				$type2 = $value['type_of_value'];
				if ($value['group'] <> 'element'){
					$dat2 = $treeclass->gettreetypes($type2);
					if (count($dat2) > 0){
						foreach ($dat2 as $value2){
							$n2 = $value2['name'];
							$treeclass->save_item_data($type2, $p2, $n2, '');
						}
					}
				}
			}
		}
	}

	$data['aditionalelements']['types'] = $treeclass->gettreetypes('block');
	$treeclass->maketree(0, $lang, $data['aditionalelements']['types']);
	$data['aditionalelements']['list'] = $treeclass->get_block_tempalate($treeclass->mas);
	$data['aditionalelements']['curent'] = $treeclass->get_block_parent($id, $name);
	
	if (isset($data['admins']['cur']['mod'][1]) && ($data['admins']['cur']['mod'][1] == 1)){
		$data['denied'] = 0;
	} else {
		$data['denied'] = $treeclass->get_page_permision($_SESSION['login_user'], $data['page']['id']);
	}
	
	if (isset($setupdata['smarty_guest_styles_template_file'])) {
		$configs = $treeclass->get_styles();
	}
	
	if ((isset($data['page']['lang'])) && ($id > 0)) {
		if (in_array($data['page']['lang'], $lang_array2)) {
			$lang = $data['page']['lang'];
			$_SESSION['lang'] = $data['page']['lang'];
			$data['tree']['lng']['cur'] = $data['page']['lang'];
		}
	} else $data['tree']['lng']['cur'] = $lang;
	
	if (isset($setupdata['languages_switch']) && ($setupdata['languages_switch'] == 1)){
		$data['active_langs'] = $treeclass->active_langs;
	} else $data['active_langs'] = "false";
	
	$data['site'] = $treeclass->_get_site_settings($id);

	if ((isset($_GET['kill']) and ($_GET['kill'] == 1)) or (isset($_POST['kill']) and ($_POST['kill'] == 1))) {
		$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
		$smarty->display('admin/kill_popup.tpl');
	}
	else {
		if (isset($act) && ($_GET["no-refresh"] != 1 && $_POST["no-refresh"] != 1)) {
			require ($setupdata['core_dir'].'/includes/referer.php');
		} else {
			$smarty->template_dir = $setupdata['smarty_admin_template_dir'];
			$setupdata['vars']['default_lang'] = $setupdata['default_languages'];
			$smarty->assign("_var", $setupdata['vars']);
			$smarty->assign("_local_var", $setupdata['local_vars']);
			$smarty->assign("tree", $tree);
			$smarty->assign("data", $data);
			if (isset($configs)) {$smarty->assign("config", $configs);}
			$smarty->assign("_http_path", $setupdata['base_href']);
			if ($is_xajax !== false){
				$xajax->processRequests();	
				$smarty->assign('xajax_javascript', $xajax->getJavascriptAdmin("_tvs_system_core_/js/","xajax.js"));
			}
			$smarty->display('admin/admin.tpl');
		}
		$err->DeleteErrorSession();
	}
?>
